package com.tbvtc.agriculturalcreditchainserver2025.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tbvtc.agriculturalcreditchainserver2025.entity.CropRecords;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.CropRecordsMapper;
import com.tbvtc.agriculturalcreditchainserver2025.service.ICropRecordsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbvtc.agriculturalcreditchainserver2025.service.IFarmlandInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 农作物生长监测记录表 服务实现类
 * </p>
 *
 * @author syh
 * @since 2025-07-22
 */
@Service
public class CropRecordsServiceImpl extends ServiceImpl<CropRecordsMapper, CropRecords> implements ICropRecordsService {

    @Resource
    private IFarmlandInfoService farmlandInfoService;
    @Override
    public List<CropRecords> getFieldArchives(String stageName, String archivesName) {
        List<CropRecords> list = list(new LambdaQueryWrapper<CropRecords>()
                // 当stageName不为空时，才添加等于条件
                .eq(StringUtils.isNotBlank(stageName), CropRecords::getReportType, stageName)
                // 当archivesName不为空时，才添加模糊查询条件
                .like(StringUtils.isNotBlank(archivesName), CropRecords::getArchivesName, archivesName)
        );
        list.forEach(item -> {
            item.setLandName(farmlandInfoService.getLandNameByFoodName(item.getCropName()));
        });

        return  list;
    }
}
